﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyPSDALayer;
using MyPS.Models;
using System.Web.Security;
using MyPS.Areas.Manage.Models;
using System.Data.SqlClient;
using System.Data;

namespace MyPS.Areas.Manage.Controllers
{
    [Authorize]
    public class ManageUsersController : Controller
    {
        private MyPSEntities db = new MyPSEntities();
        public ActionResult Index()
        {
            return View(db.Users.ToList());
        }
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                if (db.Users.Where(o => o.UserID == user.UserID).Count() > 0)
                {
                    ModelState.AddModelError("", "用户名已存在");
                }
                else
                {
                    user.CreateBy = HttpContext.User.Identity.Name;
                    user.CreateDate = DateTime.Now;
                    user.ModifyBy = HttpContext.User.Identity.Name;
                    user.ModifyDate = DateTime.Now;
                    db.Users.Add(user);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

            }

            return View(user);
        }
        public ActionResult Edit(string userid = "")
        {
            User user = db.Users.Find(userid);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                user.ModifyBy = HttpContext.User.Identity.Name;
                user.ModifyDate = DateTime.Now;
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(user);
        }
        public ActionResult Delete(string userid = "")
        {
            User user = db.Users.Find(userid);
            if (user == null)
            {
                return HttpNotFound();
            }
            return View(user);
        }
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string userid)
        {
            User user = db.Users.Find(userid);
            db.Users.Remove(user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult Role(string userid)
        {
            UserRoleModel userRole = new UserRoleModel();
            userRole.UserID = userid;
            userRole.RoleList = db.Database.SqlQuery<M_Role>(@"select a.RoleID,a.Descr from Roles a 
            where not Exists (select * from UserRoles b where a.RoleID=b.RoleID and b.UserID=@UserID)", new SqlParameter("@UserID", SqlDbType.NVarChar, 50) { Value = userid }).ToList();
            userRole.ExsistRoleList = db.Database.SqlQuery<M_UserRole>("select a.UserID,a.RoleID,b.Descr from UserRoles a with(nolock) inner join Roles b on a.RoleID=b.RoleID where a.UserID=@UserID", new SqlParameter("@UserID", SqlDbType.NVarChar, 50) { Value = userid }).ToList();
            return View(userRole);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddRole(UserRole uRole)
        {
            if (ModelState.IsValid)
            {
                db.UserRoles.Add(uRole);
                db.SaveChanges();
                return RedirectToAction("Role", new { userid = uRole.UserID });
            }
            return View(uRole);
        }

        public ActionResult RemoveRole(string userid, string roleid)
        {
            var mode = db.UserRoles.Find(userid, roleid);
            db.UserRoles.Remove(mode);
            db.SaveChanges();
            return RedirectToAction("Role", new { userid = userid });
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}